// page/component/evaluate/evaluate.js

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 0, //顾客输入的字数
    star: [0, 0, 0, 0, 0], //控制星星颜色显示
    good: false, //顾客是否赞
    submit: false, //用户是否可以提交
    level: 0, //用户评价的星级
    text: '', //用户评价的文字
    goodsId: null,
    goodsName: '商品名称',
    userID: null
  },

  //星级评价函数
  starlevel: function (e) {
    let self = this;
    // console.log(e.currentTarget.dataset.index);
    let num = e.currentTarget.dataset.index;
    let arr = self.data.star;
    // console.log(arr);
    if (num == 0) {
      self.setData({
        star: [1, 0, 0, 0, 0],
        level: num,
      })
    }
    if (num == 1) {
      self.setData({
        star: [1, 1, 0, 0, 0],
        level: num,
      })
    }
    if (num == 2) {
      self.setData({
        star: [1, 1, 1, 0, 0],
        level: num,
      })
    }
    if (num == 3) {
      self.setData({
        star: [1, 1, 1, 1, 0],
        level: num,
      })
    }
    if (num == 4) {
      self.setData({
        star: [1, 1, 1, 1, 1],
        level: num,
      })
    }
    self.setData({
      submit: true
    })
    // console.log(self.data.star);
    // console.log(self.data.submit);
    console.log(self.data.level);
  },

  //点赞函数
  Goodtrue: function () {
    // var self = this;
    this.setData({
      good: false
    })
  },
  Goodfalse: function () {
    // var self = this;
    this.setData({
      good: true
    })
  },

  //评价函数
  textChange: function (e) {
    let self = this;
    // console.log(e.detail.value.length);
    let len = e.detail.value.length;
    let t = e.detail.value;
    self.setData({
      num: len,
      text: t
    });
  },

  //用户提交函数
  submit: function () {
    let myDate = new Date();//获取系统当前时间

    let self = this;
    let text = self.data.text;
    let level = self.data.level;
    let name = app.globalData.userInfo.nickName;
    let img = app.globalData.userInfo.avatarUrl;
    console.log(name);
    console.log(img);
    wx.getStorage({
      key: 'userID',
      success(res) {
        self.setData({
          userID: res.data
        }) 
      }
    });
    let userId = this.data.userID;
    if (userId == null) {
      userId = getApp().globalData.userID
    }
    wx.request({
      url: 'https://localhost:80/evaluate/evaUpload', 
      data: {
        "evaId": null, //不处理
        "evaUser": userId, //假的
        "evaContent": text, //内容
        "evaDate": null, //假的
        "evaGoods": this.data.goodsId, //假的
        "evaState": 1, //默认不处理
        "evaLevel": level,
        "userPhoto": img,
        "userName": name
      },
      method : 'POST',
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        console.log('成功');
        wx.showToast({
          title: '评价成功',
          icon: 'success',
          duration: 2000
        })
        wx.switchTab({
          url: '../person/person'
        })
      },
      fail: function () {
        // fail
        console.log('请求失败！');
        wx.showToast({
          title: '提交失败',
          icon: 'none',
          duration: 2000
        })
      },
      complete: function () {
        // complete
      }
    })
  },
  /**  
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.num);
    let data = JSON.parse(options.num);
    console.log(data);
    console.log(data.orderDetails[0].goods.goodsId);
    let goodsName = [];
    for (let i = 0; i < data.orderDetails.length; i++)
    {
      goodsName[i] = data.orderDetails[i].goods.goodsName;
    }
    this.setData({
      goodsId: data.orderDetails[0].goods.goodsId,
      goodsName: goodsName
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})